﻿call_ally_interaction = {
	category = interaction_category_diplomacy
	desc = call_ally_interaction_desc
	interface_priority = 60
	interface = call_ally
	special_interaction = call_ally_interaction
	popup_on_receive = yes
	pause_on_receive = yes
	icon = alliance

	greeting = positive
	notification_text = CALL_ALLY_NOTIFICATION

	is_shown = {
		scope:actor = {
			is_at_war = yes
			trigger_if = { # Avoid calling Adventurers without armies or landed rulers who have been devastated
				limit = {
					is_ai = yes
				}
				scope:recipient.current_military_strength >= 100
			}
			trigger_if = {
				limit = {
					liege = scope:recipient # The recipient is actor's liege
				}
				subject_contract_has_flag = vassal_contract_liege_forced_war_override
			}
		}
		scope:recipient = {
			OR = {
				is_allied_to = scope:actor
				AND = {
					this ?= scope:actor.diarch
					scope:actor = {
						any_character_war = { diarch_callable_in_internal_war_trigger = yes }
					}
				}
				scope:actor = {
					is_vassal_of = prev
					subject_contract_has_flag = vassal_contract_liege_forced_war_override
				}
				scope:actor = {
					is_tributary_of = prev
				}
			}
		}
		scope:actor = { #If you can call them as a House Member then do that
			NOT = {
				is_character_interaction_valid = {
					recipient = scope:recipient
					interaction = call_house_member_to_war_interaction
				}
			}
		}
	}
	
	has_valid_target = {
		exists = scope:target
	}	

	has_valid_target_showing_failures_only = {
		scope:target = {
			is_war_leader = scope:actor
		}

		scope:actor = {
			trigger_if = {
				limit = { # if you're invoking your suzerain's defensive guarantee, it has to be for a defensive war
					NOT = { is_allied_to = scope:recipient }
					suzerain = scope:recipient
					subject_contract_has_flag = tributary_contract_suzerain_guarantee_override
				}
				scope:target = {
					primary_defender = scope:actor
				}
			}

			trigger_if = {
				limit = {
					scope:target = {
						OR = {
							using_cb = undirected_great_holy_war
							using_cb = directed_great_holy_war
						}
					}
				}

				trigger_if = {
					limit = {
						this = scope:target.casus_belli.primary_attacker
					}
					#if it's a religious war the recipient has to be of the same faith
					custom_description = {
						text = is_of_wrong_faith_for_holy_war
						subject = scope:actor
						object = scope:recipient
						faith = scope:recipient.faith
					}
				}
				trigger_else = {
					custom_description = {
						text = is_of_attacker_faith_in_holy_war
						subject = scope:actor
						object = scope:recipient
						scope:target = { NOT = { primary_attacker.faith = scope:recipient.faith } }
					}
				}
			}
		}

		joiner_not_already_in_another_war_with_any_target_war_participants_trigger = {
			WARRIOR = scope:actor
			JOINER = scope:recipient
		}
	}

	
	is_valid_showing_failures_only = {
		
		scope:recipient = {
			is_ruler = yes
		}

		# Only War Leaders can call allies
		scope:actor = {
			custom_description = {
				text = must_be_war_leader
				any_character_war = {
					is_war_leader = scope:actor
				}
			}
		}

		# Cannot call the same character multiple times
		scope:actor = {
			custom_description = {
				text = cannot_call_same_character_multiple_times
				subject = scope:recipient
				any_character_war = {
					ALL_FALSE = {
						was_called = scope:recipient
						is_participant = scope:recipient
					}
				}
			}
		}

		can_potentially_call_ally_trigger = {
			WARRIOR = scope:actor
			JOINER = scope:recipient
		}

		# Cannot call a character of the "wrong" religion to a Great Holy War
		scope:actor = {
			trigger_if = {
				limit = {
					NOT = { faith = scope:recipient.faith }
				}
				custom_description = {
					text = is_of_wrong_faith_for_holy_war
					subject = scope:actor
					object = scope:recipient
					any_character_war = {
						NOR = {
							using_cb = undirected_great_holy_war
							using_cb = directed_great_holy_war
						}
					}
				}
			}
		}
	}

	can_be_picked = {
		can_join_war_liege_vassal_check_trigger = { WARRIOR = scope:actor JOINER = scope:recipient }

		scope:target = {
			is_war_leader = scope:actor

			# not already in target war
			custom_description = {
				text = join_war_interaction_already_in_target_war
				NOR = {
					any_war_attacker = { this = scope:recipient }
					any_war_defender = { this = scope:recipient }
				}
			}

			# Diarchs can only be called to internal wars.
			custom_tooltip = {
				text = call_ally_interaction.tt.diarchs_called_to_internal_wars
				trigger_if = {
					limit = { scope:recipient ?= scope:actor.diarch }
					primary_defender = scope:actor
					diarch_callable_in_internal_war_trigger = yes
				}
			}
		}
		
		# You cannot call a suzerain against their own tributary
		trigger_if = {
			limit = {
				scope:target.casus_belli.primary_defender = scope:actor
				scope:target.casus_belli.primary_attacker = { is_tributary_of_suzerain_or_above = scope:recipient }
			}
			NOT = {
				scope:target.casus_belli.primary_attacker = { is_tributary_of_suzerain_or_above = scope:recipient }
			}
		}
		trigger_else_if = {
			limit = {
				scope:target.casus_belli.primary_attacker = scope:actor
				scope:target.casus_belli.primary_defender = { is_tributary_of_suzerain_or_above = scope:recipient }
			}
			NOT = {
				scope:target.casus_belli.primary_defender = { is_tributary_of_suzerain_or_above = scope:recipient }
			}
		}
		trigger_else = {}
		
		# You cannot call a tributary against their suzerain
		trigger_if = {
			limit = {
				scope:target.casus_belli.primary_defender = scope:actor
				scope:recipient = { is_tributary_of_suzerain_or_above = scope:target.casus_belli.primary_attacker }
			}
			NOT = {
				scope:recipient = { is_tributary_of_suzerain_or_above = scope:target.casus_belli.primary_attacker }
			}
		}
		trigger_else_if = {
			limit = {
				scope:target.casus_belli.primary_attacker = scope:actor
				scope:recipient = { is_tributary_of_suzerain_or_above = scope:target.casus_belli.primary_defender }
			}
			NOT = {
				scope:recipient = { is_tributary_of_suzerain_or_above = scope:target.casus_belli.primary_defender }
			}
		}
		trigger_else = {}
	}

	on_auto_accept = {
		scope:recipient = {
			trigger_event = call_ally.0001
		}
		scope:target = {
			if = {
				limit = {
					is_religious_war = yes
					scope:recipient = {
						any_active_accolade = {
							has_accolade_parameter = acclaimed_knight_piety_from_battle
						}
					}
				}
				scope:recipient = {
					add_piety = miniscule_piety_gain
				}
			}
			if = {
				limit = {
					is_religious_war = yes
					scope:recipient = {
						any_active_accolade = {
							has_accolade_parameter = acclaimed_knight_piety_from_battle_high
						}
					}
				}
				scope:recipient = {
					add_piety = minor_piety_gain
				}
			}
		}
	}
		
	on_accept = {
		call_ally_interaction_effect = yes

		save_scope_value_as = {
			name = call_ally_interaction
			value = yes
		}

		scope:actor = {
			hidden_effect = { #To nudge friendship
				if = {
					limit = {
						NOR = {
							has_relation_friend = scope:recipient
							has_relation_potential_friend = scope:recipient
						}
					}
					set_relation_potential_friend = scope:recipient
				}
			}
			trigger_event = call_ally.0100
		}
		scope:target = {
			if = {
				limit = {
					is_religious_war = yes
					scope:recipient = {
						any_active_accolade = {
							has_accolade_parameter = acclaimed_knight_piety_from_battle
						}
					}
				}
				scope:recipient = {
					add_piety = minor_piety_gain
				}
			}
			if = {
				limit = {
					is_religious_war = yes
					scope:recipient = {
						any_active_accolade = {
							has_accolade_parameter = acclaimed_knight_piety_from_battle_high
						}
					}
				}
				scope:recipient = {
					add_piety = medium_piety_gain
				}
			}
		}
	}
	
	on_decline = {
		#The war could theoretically end on the day the decline is sent
		if = {
			limit = {
				exists = scope:target
			}
			scope:target = {
				if = {
					limit = {
						is_attacker = scope:actor
					}
					scope:actor = {
						add_opinion = {
							modifier = rejected_call_to_offensive_war
							target = scope:recipient
						}
					}
					scope:recipient = {
						add_prestige_experience = major_prestige_loss
					}
					
				}
				else = {
					scope:actor = {
						add_opinion = {
							modifier = rejected_call_to_defensive_war
							target = scope:recipient
						}
					}
					scope:recipient = {
						add_prestige_experience = massive_prestige_loss
					}
				}
				if = {
					limit = {
						scope:actor = {
							has_relation_blood_brother = scope:recipient
						}
					}
					scope:recipient = {
						add_piety_experience = massive_piety_loss
						add_character_modifier = {
							modifier = broke_blood_brotherhood_modifier
							desc = broke_blood_brotherhood_modifier_acceptance_tt
							years = 25
						}
						remove_relation_blood_brother = scope:actor
					}
					scope:actor = {
						set_relation_rival = {
							target = scope:recipient
							reason = rival_blood_brother_refused_call_reason
						}
					}
				}
				# Nomad Vassals and Tributaries stop being Obedient if they have the Liege War Support / Suzerain Defensive Guarantee contract right
				if = {
					limit = {
						scope:recipient = scope:actor.obedience_target
						scope:actor = {
							OR = {
								subject_contract_has_flag = vassal_contract_liege_forced_war_override
								subject_contract_has_flag = tributary_contract_suzerain_guarantee_override
							}
						}
					}
					scope:actor = {
						set_variable = {
							name = not_obedient_declined_call
							value = scope:recipient
							years = 5
						}
					}
				}
				if = {
					limit = {
						scope:recipient = scope:actor.suzerain
						is_defender = scope:actor
						scope:actor = {
							subject_contract_has_flag = tributary_contract_suzerain_guarantee_override
						}
					}
					scope:actor = {
						end_tributary = yes #TODO_CD_MPO some sort of message here
					}
				}
				hidden_effect = {
					if = {
						limit = {
							NOT = { was_called = scope:recipient }
						}
						set_called_to = scope:recipient
					}
				}
			}
			scope:actor = {
				trigger_event = call_ally.0101
			}
			
			# If we're a clan this interaction affects unity
			refuse_call_to_arms_add_clan_unity_effect = yes
		}
	}

	auto_accept = {
		OR = {
			# Always accept a call from a Spouse
			custom_description = {
				text = "recipient_is_spouse"
				subject = scope:actor
				object = scope:recipient
				scope:recipient = {
					is_spouse_of = scope:actor
					is_ai = yes
				}
			}
			# Always accept a call from the Heir
			custom_description = {
				text = "is_recipients_player_heir"
				subject = scope:actor
				object = scope:recipient
				
				exists = scope:recipient.player_heir
				scope:recipient = {
					player_heir = scope:actor
					is_close_or_extended_family_of = scope:actor # Only for family
					is_ai = yes
				}
			}
			# Always accept a call from the one you are heir of
			custom_description = {
				text = "is_player_heir"
				subject = scope:recipient
				object = scope:actor

				exists = scope:actor.player_heir
				scope:actor.player_heir = scope:recipient
				scope:actor = { # Shouldn't be able to force your liege to join just because you've not got any family...
					is_close_or_extended_family_of = scope:recipient
				}
				scope:recipient = {
					is_ai = yes
				}
			}
		}
	}
	
	ai_accept = {
		base = -75 # 20

		modifier = {  # Conquerors do not help anyone but themselves
			add = -1000
			desc = NO_FRIVOLOUS_ACTIVITIES_REASON
			scope:recipient = { ai_has_conqueror_personality = yes }
		}

		modifier = {  # AI's that need to save gold will not help their lesser AI allies, unless it's a player war
			add = -1000
			desc = NO_FRIVOLOUS_ACTIVITIES_REASON
			scope:actor = {
				is_ai = yes
				primary_title.tier < scope:recipient.primary_title.tier
			}
			scope:target.casus_belli = {
				primary_attacker = { is_ai = yes }
				primary_defender = { is_ai = yes }
			}
			scope:recipient = { ai_should_focus_on_building_in_their_capital = yes }
		}

		modifier = {  # Refuse call to conflicts of little benefit
			add = -1000
			desc = WONT_FIGHT_MEANINGLESS_REASON
			trigger_if = {
				limit = { exists = scope:target }
				scope:target = { using_cb = fp2_border_raid }
				scope:recipient = { # Unless they like going on raids
					NOR = {
						has_trait = viking
						has_trait = reaver
					}
				}
			}
			trigger_else = { always = no }
		}

		modifier = {  # Refuse call against Heir
			add = -1000
			exists = scope:recipient.player_heir
			trigger_if = {
				limit = { scope:target.casus_belli.primary_defender = scope:actor }
				scope:target.casus_belli.primary_attacker = scope:recipient.player_heir
			}
			trigger_else = {
				scope:target.casus_belli.primary_defender = scope:recipient.player_heir
			}
			desc = WONT_FIGHT_HEIR_REASON
		}

		modifier = {  # Refuse call against Spouse
			add = -1000
			scope:recipient = {
				trigger_if = {
					limit = { scope:target.casus_belli.primary_defender = scope:actor }
					any_spouse = {
						this = scope:target.casus_belli.primary_attacker
					}
				}
				trigger_else = {
					any_spouse = {
						this = scope:target.casus_belli.primary_defender
					}
				}
				
			}
			desc = WONT_FIGHT_SPOUSE_REASON
		}
		
		modifier = {  # Refuse call against Suzerain if Obedient
			add = -1000
			trigger_if = {
				limit = {
					exists = scope:recipient.suzerain
					trigger_if = {
						limit = { scope:target.casus_belli.primary_defender = scope:actor }
						scope:recipient.suzerain = scope:target.casus_belli.primary_attacker
					}
					trigger_else = {
						scope:recipient.suzerain = scope:target.casus_belli.primary_defender
					}
					scope:recipient = {
						exists = obedience_target
						is_obedient = yes
					}
				}
			}
			trigger_else = { always = no }
			desc = WONT_FIGHT_OBEDIENT_REASON
		}

		opinion_modifier = { # Opinion Factor
			who = scope:recipient
			opinion_target = scope:actor
			multiplier = 1.0
			desc = AI_OPINION_REASON
		}

		# Honor factor
		ai_value_modifier = {
			ai_honor = 0.5 # 1
			min = 0
		}

		modifier = {  # Tends to join defensive wars
			add = 25 # 50
			scope:target.casus_belli.primary_defender = scope:actor
			desc = DEFENSIVE_WAR_REASON
		}
		
		modifier = { # Same language
			add = 5
			desc = speaks_same_language_interaction_reason
			trigger = {
				scope:actor = {
					knows_language_of_culture = scope:recipient.culture
				}
			}
		}
		
		modifier = { # Shared Court Language (non-spoken)
			add = 10
			desc = foreign_realm_court_language_non_spoken_interaction_reason
			trigger = {
				scope:actor = {
					has_royal_court = yes
					has_dlc_feature = royal_court
					NOT = { knows_court_language_of = this }
				}
				NOT = {
					scope:actor.top_liege = scope:recipient.top_liege
				}
				OR = {
					AND = {
						scope:recipient = {
							has_royal_court = yes
						}
						scope:actor = { # You share Court Language
							has_same_court_language = scope:recipient
						}
					}
					
					scope:recipient = { # The recipient doesn't have a royal court, but their liege does
						is_independent_ruler = no
						has_royal_court = no
						liege = {
							has_royal_court = yes
							has_same_court_language = scope:actor
						}
					}
					scope:recipient = { # The recipient doesn't have a royal court, but their liege's liege does, while their liege doesn't have a royal court
						is_independent_ruler = no
						has_royal_court = no
						exists = liege.liege
						liege = {
							is_independent_ruler = no
							has_royal_court = no
						}
						liege.liege = {
							has_royal_court = yes
							has_same_court_language = scope:actor
						}
					}
					scope:recipient = { # The recipient doesn't have a royal court, but their liege's liege's liege does, while their liege doesn't have a royal court
						is_independent_ruler = no
						has_royal_court = no
						exists = liege.liege
						exists = liege.liege.liege
						liege = {
							is_independent_ruler = no
							has_royal_court = no
						}
						liege.liege = {
							is_independent_ruler = no
							has_royal_court = no
						}
						liege.liege.liege = {
							has_royal_court = yes
							has_same_court_language = scope:actor
						}
					}
					scope:recipient = { # The recipient doesn't have a royal court, but their liege's liege's liege's liege does, while their liege doesn't have a royal court
						is_independent_ruler = no
						has_royal_court = no
						exists = liege.liege
						exists = liege.liege.liege
						exists = liege.liege.liege.liege
						liege = {
							is_independent_ruler = no
							has_royal_court = no
						}
						liege.liege = {
							is_independent_ruler = no
							has_royal_court = no
						}
						liege.liege.liege = {
							is_independent_ruler = no
							has_royal_court = no
						}
						liege.liege.liege.liege = {
							has_royal_court = yes
							has_same_court_language = scope:actor
						}
					}
				}
			}
		}
		
		modifier = { # Shared Court Language (spoken)
			add = 30
			desc = foreign_realm_court_language_spoken_interaction_reason
			trigger = {
				scope:actor = {
					has_royal_court = yes
					has_dlc_feature = royal_court
					knows_court_language_of = this
				}
				NOT = {
					scope:actor.top_liege = scope:recipient.top_liege
				}
				OR = {
					AND = {
						scope:recipient = {
							has_royal_court = yes
						}
						scope:actor = { # You share Court Language
							has_same_court_language = scope:recipient
						}
					}
					
					scope:recipient = { # The recipient doesn't have a royal court, but their liege does
						is_independent_ruler = no
						has_royal_court = no
						liege = {
							has_royal_court = yes
							has_same_court_language = scope:actor
						}
					}
					scope:recipient = { # The recipient doesn't have a royal court, but their liege's liege does, while their liege doesn't have a royal court
						is_independent_ruler = no
						has_royal_court = no
						exists = liege.liege
						liege = {
							is_independent_ruler = no
							has_royal_court = no
						}
						liege.liege = {
							has_royal_court = yes
							has_same_court_language = scope:actor
						}
					}
					scope:recipient = { # The recipient doesn't have a royal court, but their liege's liege's liege does, while their liege doesn't have a royal court
						is_independent_ruler = no
						has_royal_court = no
						exists = liege.liege
						exists = liege.liege.liege
						liege = {
							is_independent_ruler = no
							has_royal_court = no
						}
						liege.liege = {
							is_independent_ruler = no
							has_royal_court = no
						}
						liege.liege.liege = {
							has_royal_court = yes
							has_same_court_language = scope:actor
						}
					}
					scope:recipient = { # The recipient doesn't have a royal court, but their liege's liege's liege's liege does, while their liege doesn't have a royal court
						is_independent_ruler = no
						has_royal_court = no
						exists = liege.liege
						exists = liege.liege.liege
						exists = liege.liege.liege.liege
						liege = {
							is_independent_ruler = no
							has_royal_court = no
						}
						liege.liege = {
							is_independent_ruler = no
							has_royal_court = no
						}
						liege.liege.liege = {
							is_independent_ruler = no
							has_royal_court = no
						}
						liege.liege.liege.liege = {
							has_royal_court = yes
							has_same_court_language = scope:actor
						}
					}
				}
			}
		}

		modifier = {  # Serious diarchs wish to support their liege
			add = 50
			desc = ENTRENCHED_REGENT_WILL_SUPPORT_LIEGE
			trigger_if = {
				limit = { scope:recipient ?= scope:actor.diarch }
				scope:actor = { has_diarchy_parameter = diarchy_type_is_entrenched_regency }
			}
			trigger_else = { always = no }
		}		

		compare_modifier = { # Likes fighting infidels
			trigger = {
				scope:recipient.faith = scope:actor.faith
				OR = {
					AND = {
						scope:target.casus_belli.primary_attacker = {
							this = scope:actor
							faith = {
								faith_hostility_level = {
									target = scope:target.casus_belli.primary_defender.faith
									value >= religious_cb_enabled_hostility_level
								}
							}
						}
					}
					AND = {
						scope:target.casus_belli.primary_defender = {
							this = scope:actor
							faith = {
								faith_hostility_level = {
									target = scope:target.casus_belli.primary_defender.faith
									value >= religious_cb_enabled_hostility_level
								}
							}
						}
					}
				}
			}
			target = scope:recipient
			value = ai_zeal
			desc = "ZEAL_AGAINST_INFIDELS"
			min = 0
			multiplier = 0.5
		}

		modifier = {  # Reluctant to attack another ally
			add = -50
			scope:target.casus_belli.primary_attacker = scope:actor
			scope:recipient = {
				is_allied_to = scope:target.casus_belli.primary_defender
			}
			desc = ATTACK_ON_ALLY_REASON
		}

		modifier = {  # Reluctant to defend against another ally
			add = -25
			scope:target.casus_belli.primary_defender = scope:actor
			scope:recipient = {
				is_allied_to = scope:target.casus_belli.primary_attacker
			}
			desc = WAR_WITH_ALLY_REASON
		}

		modifier = {  # Reluctant to join wars against religious brethren.
			add = -50
			NOT = { scope:recipient.faith = scope:actor.faith }
			scope:target.casus_belli.primary_attacker = scope:actor
			scope:target.casus_belli.primary_defender.faith = scope:recipient.faith
			scope:target.casus_belli.war = {
				OR = {
					using_cb = minor_religious_war
					using_cb = religious_war
					using_cb = major_religious_war
					using_cb = undirected_great_holy_war
					using_cb = directed_great_holy_war
				}
			}
			desc = WONT_ATTACK_RELIGIOUS_BRETHREN_REASON
		}

		modifier = { # Reluctant to join against Friends
			add = -50
			trigger_if = {
				limit = {
					scope:target.casus_belli.primary_defender = scope:actor
				}
				has_relation_friend = scope:target.casus_belli.primary_attacker
			}
			trigger_else = {
				has_relation_friend = scope:target.casus_belli.primary_defender
			}
			desc = WONT_FIGHT_FRIEND
		}

		modifier = { # Reluctant to join against Best Friends
			add = -100
			trigger_if = {
				limit = {
					scope:target.casus_belli.primary_defender = scope:actor
				}
				has_relation_best_friend = scope:target.casus_belli.primary_attacker
			}
			trigger_else = {
				has_relation_best_friend = scope:target.casus_belli.primary_defender
			}
			desc = WONT_FIGHT_BEST_FRIEND
		}
		modifier = { # Reluctant to join against Lovers
			add = -100
			trigger_if = {
				limit = {
					scope:target.casus_belli.primary_defender = scope:actor
				}
				has_relation_lover = scope:target.casus_belli.primary_attacker
			}
			trigger_else = {
				has_relation_lover = scope:target.casus_belli.primary_defender
			}
			desc = WONT_FIGHT_LOVER
		}
		modifier = { # Reluctant to join against Soulmate
			add = -200
			trigger_if = {
				limit = {
					scope:target.casus_belli.primary_defender = scope:actor
				}
				has_relation_soulmate = scope:target.casus_belli.primary_attacker
			}
			trigger_else = {
				has_relation_soulmate = scope:target.casus_belli.primary_defender
			}
			desc = WONT_FIGHT_SOULMATE
		}
		modifier = { # Reluctant to join tributaries
			add = -200
			scope:actor = {
				is_tributary_of = scope:recipient
			}
			scope:recipient = {
				NOR = {
					has_relation_lover = scope:actor
					has_relation_friend = scope:actor
					is_allied_to = scope:actor
					subject_contract_has_flag = tributary_contract_suzerain_guarantee_override
				}
			}
			desc = WONT_FIGHT_MEANINGLESS_REASON
		}
		modifier = {  # Refuse call for hostages' safety
			any_home_court_hostage = {
				warden = {
					OR = {
						is_at_war_with = scope:actor
						any_ally = { is_at_war_with = scope:actor }
					}
				}
			}
			add = {
				value = 0
				every_home_court_hostage = {
					limit = {
						warden = {
							OR = {
								is_at_war_with = scope:actor
								any_ally = { is_at_war_with = scope:actor }
							}
						}
					}
					subtract = call_to_arms_hostage_value
				}
				multiply = 0.25
			}
			desc = HOSTAGE_ENEMY_CALL_TO_ARMS_REASON
		}

		fp3_struggle_resist_allied_wars_modifier = yes
	}

	# Score above 0 means the AI will call this ally. Note that DESIRED_WAR_SIDE_STRENGTH still applies and can prevent calling the ally
	ai_will_do = {
		base = 100
		
		modifier = { # If the player is at war, do not bother them with offensive calls
			scope:target.casus_belli.primary_attacker = scope:actor
			scope:recipient = {
				is_ai = no
				any_character_war = {
					primary_defender = scope:recipient
				}
			}
			factor = 0
		}
		
		modifier = { # Do not call the player if they are in debt
			scope:recipient = {
				is_ai = no
				gold <= -1
			}
			factor = 0
		}
		
		modifier = { # Don't call players in against their heirs (they wouldn't accept the reverse situation... don't be a hypocrite)
			exists = scope:recipient.player_heir
			scope:recipient = {			
				is_ai = no
			}
			trigger_if = {
				limit = { scope:target.casus_belli.primary_defender = scope:actor }
				scope:target.casus_belli.primary_attacker = scope:recipient.player_heir
			}
			trigger_else = {
				scope:target.casus_belli.primary_defender = scope:recipient.player_heir
			}
			factor = 0
		}
		
		modifier = { # Same with spouses
			scope:recipient = {			
				is_ai = no
			}
			trigger_if = {
				limit = { scope:target.casus_belli.primary_defender = scope:actor }
				any_spouse = {
					this = scope:target.casus_belli.primary_attacker
				}
			}
			trigger_else = {
				any_spouse = {
					this = scope:target.casus_belli.primary_defender
				}
			}
			factor = 0
		}
		
		modifier = { # Don't call brand-new allies into old wars, at least until some time has elapsed.
			has_variable = delay_calling_recent_ally
			var:delay_calling_recent_ally = scope:recipient
			scope:target = {
				war_days >= 30
			}
			factor = 0

		}

		modifier = { # If the recipient is allied to both the attacker and the defender, the attacker shouldn't call
			scope:target.casus_belli.primary_attacker = scope:actor
			scope:target.casus_belli.primary_defender = {
				is_allied_to = scope:recipient
			}
			factor = 0
		}

		modifier = { # Don't call allies into offensive wars, if the recipient doesn't have enough opinion 
			scope:target.casus_belli.primary_attacker = scope:actor
			scope:recipient = {
				is_ai = no
				opinion = {
					target = scope:actor
					value < 75
				}
			}
			factor = 0
		}

		modifier = { # Don't call allies into defensive wars, if the recipient doesn't have enough opinion 
			NOT = { scope:target.casus_belli.primary_attacker = scope:actor }
			scope:recipient = {
				is_ai = no
				opinion = {
					target = scope:actor
					value < 50
				}
			}
			factor = 0
		}
	}
}

negotiate_alliance_interaction = {
	category = interaction_category_diplomacy
	ai_min_reply_days = 4
	ai_max_reply_days = 9
	icon = alliance

	send_name = negotiate_alliance_interaction

	interface_priority = 60
	desc = negotiate_alliance_interaction_desc

	greeting = positive
	notification_text = NEGOTIATE_ALLIANCE_NOTIFICATION

	is_shown = {
		scope:recipient = {
			NOT = { this = scope:actor }
			NOT = { is_allied_to = scope:actor }
			is_ruler = yes
		}
		scope:actor = {
			is_ruler = yes
			OR = {
				is_ai = no
				highest_held_title_tier >= 2
			}
			#childhood friendship with hostage event, where they swear to be friends forever
			OR = {
				is_any_family_relation_or_spouse_trigger = { CHARACTER = scope:recipient }
				hostage_oath_of_friendship_trigger = yes
				#Parents/spouse were blood brothers
				has_opinion_modifier = {
					modifier = blood_brother_close_family_opinion
					target = scope:recipient
				}
			}
		}
	}
	
	is_valid_showing_failures_only = {
		scope:recipient = {
			is_playable_character = yes
			is_imprisoned = no
		}
		scope:actor = {
			is_playable_character = yes
			is_imprisoned = no
			NOT = {
				is_at_war_with = scope:recipient
			}
		}
		scope:recipient = { is_busy_in_events_localised = yes }
		custom_description = {
			text = "has_already_rejected_renegotiation"
			subject = scope:recipient
			NOT = {
				scope:actor = {
					has_opinion_modifier = {
						modifier = refused_alliance_opinion
						target = scope:recipient
					}
				}
			}
		}
	}

	is_valid = { #To give some clarity to the tooltip that states "blood ties"

		OR = {
			#Ruler has the defensive negotiations perk
			scope:actor = { has_perk = defensive_negotiations_perk }
			#Ruler is married to other ruler.
			scope:actor = { is_spouse_of = scope:recipient }
			#Ruler is relative of other ruler
			scope:actor = { is_parent_of = scope:recipient }
			scope:actor = { is_grandparent_of = scope:recipient }
			scope:actor = { is_great_grandparent_of = scope:recipient }
			scope:actor = { is_child_of = scope:recipient }
			scope:actor = { is_grandchild_of = scope:recipient }
			scope:actor = { is_great_grandchild_of = scope:recipient }
			scope:actor = { is_uncle_or_aunt_of = scope:recipient }
			scope:actor = { is_nibling_of = scope:recipient }
			scope:actor = { is_sibling_of = scope:recipient }
			#childhood friendship with hostage event, where they swear to be friends forever
			scope:actor = {
				hostage_oath_of_friendship_trigger = yes
			}
			#Parents/spouse were blood brothers
			scope:actor = {
				has_opinion_modifier = {
					modifier = blood_brother_close_family_opinion
					target = scope:recipient
				}
			}

			custom_description = {
				text = rulers_family_is_married_to_other_ruler_trigger
				subject = scope:recipient
				object = scope:actor
				#Ruler's child/parent/sibling/grandchild/grandparent/uncle/aunt/niece/nephew is married to other ruler.
				scope:recipient = {
					any_spouse = {
						OR = {
							is_close_family_of = scope:actor
							is_nibling_of = scope:actor
							is_uncle_or_aunt_of = scope:actor
						}
					}
				}
			}

			custom_description = {
				text = rulers_family_is_married_to_other_ruler_trigger
				subject = scope:actor
				object = scope:recipient
				#Other ruler's child/parent/sibling/grandchild/grandparent/uncle/aunt/niece/nephew is married to first ruler.
				scope:actor = {
					any_spouse = {
						OR = {
							is_close_family_of = scope:recipient
							is_nibling_of = scope:recipient
							is_uncle_or_aunt_of = scope:recipient
						}
					}
				}
			}

			custom_description = {
				text = rulers_child_parent_sibling_is_married_to_other_rulers_trigger
				subject = scope:recipient
				object = scope:actor
				# Ruler's child/parent/sibling is married to other ruler's child/parent/sibling.
				scope:actor = {
					any_close_family_member = {
						OR = {
							is_parent_of = scope:actor
							is_child_of = scope:actor
							is_sibling_of = scope:actor
						}
						any_spouse = {
							OR = {
								is_parent_of = scope:recipient
								is_child_of = scope:recipient
								is_sibling_of = scope:recipient
							}
						}
					}
				}
			}

			custom_description = {
				text = hostage_oath_of_friendship_trigger
				subject = scope:recipient
				object = scope:actor
				#Ruler's child/parent/sibling/grandchild/grandparent/uncle/aunt/niece/nephew is married to other ruler.
				scope:actor = {
					hostage_oath_of_friendship_trigger = yes
				}
			}
		}
	}

	on_auto_accept = {
		scope:recipient = {
			trigger_event = char_interaction.0234
		}
	}

	#Use hook
	send_option = {
		is_valid = {
			scope:actor = {
				has_usable_hook = scope:recipient
			}
		}
		flag = hook	
		localization = GENERIC_SPEND_A_HOOK
	}
	should_use_extra_icon = {
		scope:actor = { has_usable_hook = scope:recipient }
	}
	extra_icon = "gfx/interface/icons/character_interactions/hook_icon.dds"

	# Admin Gov can spend Influence to increase AI acceptance towards other same-realm admin characters
	send_option = {
		is_shown = {
			scope:actor = {
				government_allows = administrative
			}
			scope:recipient = {
				top_liege = scope:actor.top_liege
				government_allows = administrative
			}
		}
		is_valid = {
			scope:actor = {
				influence >= major_influence_value
			}
			custom_tooltip = {
				text = ALREADY_USING_HOOK
				NOT = { scope:hook = yes }
			}
		}
		flag = influence	
		localization = SPEND_INFLUENCE
	}

	send_options_exclusive = no
		
	on_accept = {
		scope:actor = {
			trigger_event = char_interaction.0235

			stress_impact = {
				shy = minor_stress_impact_gain
			}

			if = {
				limit = {
					scope:hook = yes
					has_hook = scope:recipient
				}
				use_hook = scope:recipient
			}
			if = {
				limit = {
					scope:influence = yes
				}
				change_influence = major_influence_loss
			}
		}

		scope:recipient = {
			custom_tooltip = negotiate_alliance_effect_recipient_tooltip
		}

		#Set up alliance, depending on relation between rulers.
		hidden_effect = {
			#Prioritize marriage between rulers and/or direct blood relation between rulers first.
			if = {
				limit = {
					scope:actor = {
						OR = {
							is_spouse_of = scope:recipient
							is_child_of = scope:recipient
							is_parent_of = scope:recipient
							is_sibling_of = scope:recipient
							is_grandparent_of = scope:recipient
							is_great_grandparent_of = scope:recipient
							is_grandchild_of = scope:recipient
							is_great_grandchild_of = scope:recipient
							is_uncle_or_aunt_of = scope:recipient
							is_nibling_of = scope:recipient
						}
					}
				}
				scope:actor = {
					create_alliance = {
						target = scope:recipient
						allied_through_owner = scope:actor
						allied_through_target = scope:recipient
					}
				}
			}
			#...Then marriage between close family members and either actor or recipient.
			else_if = {
				limit = {
					scope:recipient = {
						any_spouse = {
							OR = {
								is_close_family_of = scope:actor
								is_nibling_of = scope:actor
								is_uncle_or_aunt_of = scope:actor
							}
						}
					}
				}
				scope:recipient = {
					random_spouse = {
						limit = {
							OR = {
								is_close_family_of = scope:actor
								is_nibling_of = scope:actor
								is_uncle_or_aunt_of = scope:actor
							}
						}
						save_scope_as = recipient_spouse
					}
				}
				scope:actor = {
					create_alliance = {
						target = scope:recipient
						allied_through_owner = scope:recipient_spouse
						allied_through_target = scope:recipient
					}
				}
			}
			else_if = {
				limit = {
					scope:actor = {
						any_spouse = {
							OR = {
								is_close_family_of = scope:recipient
								is_nibling_of = scope:recipient
								is_uncle_or_aunt_of = scope:recipient
							}
						}
					}
				}
				scope:actor = {
					random_spouse = {
						limit = {
							OR = {
								is_close_family_of = scope:recipient
								is_nibling_of = scope:recipient
								is_uncle_or_aunt_of = scope:recipient
							}
						}
						save_scope_as = actor_spouse
					}
				}
				scope:actor = {
					create_alliance = {
						target = scope:recipient
						allied_through_owner = scope:actor
						allied_through_target = scope:actor_spouse
					}
				}
			}
			#...And finally marriage between close relatives and close relatives.
			else_if = {
				limit = {
					scope:actor = {
						any_close_family_member = {
							OR = {
								is_child_of = scope:actor
								is_parent_of = scope:actor
								is_sibling_of = scope:actor
							}
							any_spouse = {
								OR = {
									is_child_of = scope:recipient
									is_parent_of = scope:recipient
									is_sibling_of = scope:recipient
								}
							}
						}
					}
				}
				scope:actor = {
					random_close_family_member = {
						limit = {
							OR = {
								is_child_of = scope:actor
								is_parent_of = scope:actor
								is_sibling_of = scope:actor
							}
							any_spouse = {
								OR = {
									is_child_of = scope:recipient
									is_parent_of = scope:recipient
									is_sibling_of = scope:recipient
								}
							}
						}
						save_scope_as = actor_spouse
						random_spouse = {
							limit = {
								OR = {
									is_child_of = scope:recipient
									is_parent_of = scope:recipient
									is_sibling_of = scope:recipient
								}
							}
							save_scope_as = recipient_spouse
						}
					}
				}
				scope:actor = {
					create_alliance = {
						target = scope:recipient
						allied_through_owner = scope:actor_spouse
						allied_through_target = scope:recipient_spouse
					}
				}
			}
			else_if = {
				limit = {
					scope:actor = { has_perk = defensive_negotiations_perk }
				}
				scope:actor = {
					create_alliance = {
						target = scope:recipient
						allied_through_owner = scope:actor
						allied_through_target = scope:recipient
					}
				}
				scope:recipient = { # This opinion modifier controls the interaction, and is removed when breaking the alliance in any way (though on_actions)
					add_opinion = {
						modifier = perk_negotiated_alliance_opinion
						target = scope:actor
					}
				}
			}
			else_if = {
				limit = {
					scope:actor = {
						hostage_oath_of_friendship_trigger = yes
					}
				}
				scope:actor = {
					create_alliance = {
						target = scope:recipient
						allied_through_owner = scope:actor
						allied_through_target = scope:recipient
					}
				}
			}
		}

		add_clan_unity_interaction_effect = {
			CHARACTER = scope:actor
			TARGET = scope:recipient
			VALUE = minor_unity_gain
			DESC = clan_unity_alliance.desc
			REVERSE_NON_HOUSE_TARGET = no
		}
	}
	
	on_decline = {
		scope:actor = {
			trigger_event = char_interaction.0236
		}
		scope:actor = {
			add_opinion = {
				modifier = refused_alliance_opinion
				target = scope:recipient
			}
		}

		add_clan_unity_interaction_effect = {
			CHARACTER = scope:actor
			TARGET = scope:recipient
			VALUE = minor_unity_loss
			DESC = clan_unity_alliance_refusal.desc
			REVERSE_NON_HOUSE_TARGET = no
		}
	}

	auto_accept = {
		custom_description = {
			text = "spending_hook"
			subject = scope:actor
			object = scope:recipient
			scope:hook = yes
		}
	}
	ai_accept = {
		base = 0

		# Tier Difference 	(+/-)
		# Inheritance 		(+)
		# Opinion 			(+/-)
		# Dynasty Prestige. (+)
		# Different Faith.	(-)
		# Liege is asking 	(+)
		# Dread 			(+)
		# Power difference 	(+/-)
		# Claimants (+/-)
		# Same House (+)
		# Declared war opinion
		# Actor is at war penalty
		
		modifier = {
			add = 20
			scope:recipient = {
				is_close_family_of = scope:actor
			}
			desc = CLOSE_FAMILY_REASON
		}
		
		modifier = {
			add = 10
			scope:recipient = {
				NOT = { is_close_family_of = scope:actor }
				is_extended_family_of = scope:actor
			}
			desc = EXTENDED_FAMILY_REASON
		}

		modifier = {
			add = -20
			scope:recipient = {
				NOT = { is_close_or_extended_family_of = scope:actor }
				any_spouse = {
					NOT = { is_close_family_of = scope:actor }
					is_extended_family_of = scope:actor
				}
			}
			desc = DISTANT_FAMILY_CONNECTION_REASON
		}

		# Tier Differences
		modifier = {
			scope:actor = {
				tier_difference = {
					target = scope:recipient
					value >= 1
				}
			}
			add = {
				value = 30
				if = {
					limit = {
						tier_difference = {
							target = scope:recipient
							value = 2
						}
					}
					add = 30
				}
				if = {
					limit = {
						tier_difference = {
							target = scope:recipient
							value = 3
						}
					}
					add = 60
				}
				if = {
					limit = {
						tier_difference = {
							target = scope:recipient
							value = 4
						}
					}
					add = 90
				}
				if = {
					limit = {
						tier_difference = {
							target = scope:recipient
							value = 5
						}
					}
					add = 120
				}
			}
			desc = RANK_DIFFERENCE_REASON
		}
		modifier = {
			scope:actor = {
				tier_difference = {
					target = scope:recipient
					value <= -1
				}
			}
			add = {
				value = -30
				if = {
					limit = {
						tier_difference = {
							target = scope:recipient
							value = -2
						}
					}
					add = -30
				}
				if = {
					limit = {
						tier_difference = {
							target = scope:recipient
							value = -3
						}
					}
					add = -60
				}
				if = {
					limit = {
						tier_difference = {
							target = scope:recipient
							value = -4
						}
					}
					add = -90
				}
				if = {
					limit = {
						tier_difference = {
							target = scope:recipient
							value = -5
						}
					}
					add = -120
				}
			}
			desc = RANK_DIFFERENCE_REASON
		}
		modifier = {
			scope:recipient = {
				has_relation_lover = scope:actor
			}
			add = {
				value = 25
			}
			desc = WE_ARE_LOVERS
		}		
		modifier = {
			scope:recipient = {
				has_relation_friend = scope:actor
			}
			add = {
				value = 25
			}
			desc = WE_ARE_FRIENDS
		}
		modifier = {
			scope:recipient = {
				is_heir_of = scope:actor
			}
			scope:actor = {
				tier_difference = {
					target = scope:recipient
					value > 0
				}
			}
			add = {
				value = 10
				if = {
					limit = {
						scope:actor = { player_heir = scope:recipient }
					}
					add = 10
				}
			}
			desc = IS_HEIR_REASON
		}
		opinion_modifier = { # More likely to accept if Recipient likes the Actor
			who = scope:recipient
			opinion_target = scope:actor
			multiplier = 0.25
			min = 0
			desc = AI_OPINION_REASON
		}
		opinion_modifier = { # Less likely to accept if Recipient dislikes the Actor
			who = scope:recipient
			opinion_target = scope:actor
			multiplier = 1
			max = 0
			desc = AI_OPINION_REASON
		}

		modifier = {
			add = 20
			scope:recipient = {
				has_trait = loyal
			}
			desc = INTERACTION_LOYAL
		}

		modifier = {
			add = 20
			scope:recipient = {
				has_trait = trusting
			}
			desc = INTERACTION_TRUSTING
		}

		modifier = {
			add = 5
			scope:recipient = {
				has_trait = craven
			}
			desc = INTERACTION_CRAVEN
		}

		modifier = {
			add = -20
			scope:recipient = {
				has_trait = paranoid
			}
			desc = INTERACTION_PARANOID
		}

		modifier = {
			add = -20
			scope:recipient = {
				has_trait = arrogant
			}
			desc = INTERACTION_ARROGANT
		}

		modifier = {
			add = -20
			scope:recipient = {
				has_trait = ambitious
			}
			desc = INTERACTION_AMBITIOUS
		}

		modifier = {
			add = -40
			scope:recipient = {
				has_trait = disloyal
			}
			desc = INTERACTION_DISLOYAL
		}

		modifier = {
			add = -10
			scope:recipient = {
				has_trait = fickle
			}
			desc = INTERACTION_FICKLE
		}

		# Rivalry
		modifier = { # Say no to rivals
			add = -100
			scope:recipient = {
				has_relation_rival = scope:actor
				NOT = { has_relation_nemesis = scope:actor }
			}
			desc = "ACTOR_RIVAL_TO_ME_REASON"
		}
		modifier = { # Say no to nemesis
			add = -300
			scope:recipient = {
				has_relation_nemesis = scope:actor
			}
			desc = "ACTOR_NEMESIS_TO_ME_REASON"
		}
		
		# Dynasty Prestige Levels
		modifier = {
			add = -5
			desc = AI_DYNASTY_PRESTIGE_REASON
			scope:actor = { has_dynasty = yes }
			NOT = { scope:actor.dynasty = scope:recipient.dynasty }
			scope:actor.dynasty = { dynasty_prestige_level = 0 }
		}
		modifier = {
			desc = AI_DYNASTY_PRESTIGE_REASON
			scope:actor = { has_dynasty = yes }
			NOT = { scope:actor.dynasty = scope:recipient.dynasty }
			scope:actor.dynasty = { dynasty_prestige_level >= 2 }
			add = {
				value = 10
				if = {
					limit = {
						scope:actor.dynasty = { dynasty_prestige_level >= 3 }
					}
					add = 5
				}
				if = {
					limit = {
						scope:actor.dynasty = { dynasty_prestige_level >= 4 }
					}
					add = 5
				}
				if = {
					limit = {
						scope:actor.dynasty = { dynasty_prestige_level >= 5 }
					}
					add = 5
				}
				if = {
					limit = {
						scope:actor.dynasty = { dynasty_prestige_level >= 6 }
					}
					add = 5
				}
				if = {
					limit = {
						scope:actor.dynasty = { dynasty_prestige_level >= 7 }
					}
					add = 5
				}
				if = {
					limit = {
						scope:actor.dynasty = { dynasty_prestige_level >= 8 }
					}
					add = 5
				}
				if = {
					limit = {
						scope:actor.dynasty = { dynasty_prestige_level >= 9 }
					}
					add = 5
				}
				if = {
					limit = {
						scope:actor.dynasty = { dynasty_prestige_level >= 10 }
					}
					add = 5
				}
			}
		}
		modifier = { #Faith compatibility
			scope:recipient.faith = {
				faith_hostility_level = {
					target = scope:actor.faith
					value > faith_fully_accepted_level
				}
			}
			NOT = {
				scope:recipient.ai_zeal <= -50
			}
			add = {
				value = -10
				if = {
					limit = {
						# Exempt certain struggle phases.
						NOT = { is_struggle_parameter_active_interfaith_marriages_available_between_involved_characters_trigger = yes }
					}
					if = {
						limit = {
							scope:recipient.faith = {
								faith_hostility_level = {
									target = scope:actor.faith
									value >= faith_astray_level
								}
							}
						}
						subtract = 20
					}
					if = {
						limit = {
							scope:recipient.faith = {
								faith_hostility_level = {
									target = scope:actor.faith
									value >= faith_hostile_level
								}
							}
						}
						subtract = 20
					}
					if = {
						limit = {
							scope:recipient.faith = {
								faith_hostility_level = {
									target = scope:actor.faith
									value >= faith_evil_level
								}
							}
						}
						subtract = 50
					}
					if = {
						limit = {
							scope:recipient.ai_zeal <= -25
						}
						divide = 2
					}
					else_if = {
						limit = {
							scope:recipient.ai_zeal >= 50
						}
						multiply = 2
					}										
					if = {
						limit = {
							scope:recipient.faith = { has_doctrine = doctrine_pluralism_pluralistic }
						}
						divide = 2
					}
				}
			}
			desc = MARRY_DIFFERENT_FAITH_REASON
		}

		modifier = { # A Recipient is more likely to agree to an alliance requested by his liege.
			add = 20
			scope:recipient = {
				target_is_liege_or_above = scope:actor
			}
			desc = LIEGE_REASON
		}
		
		modifier = {
	  	  	add = {
				value = 1
				subtract = {
					value = scope:recipient.max_military_strength # For foreign diplomacy, consider recipients max
					divide = { value = scope:actor.current_military_strength min = 1 }
				}
				multiply = 20
				min = -1000
				max = 100
	  		}
			desc = AI_MILITARY_BALANCE_REASON
		}
		# Recipient is a Claimant to Actor's titles.
		modifier = {
			scope:recipient = {
				any_claim = {
					holder = scope:actor
				}
				NOR = {
					is_heir_of = scope:actor
					ai_greed <= -25
				}
			}
			add = {
				value = -50
				if = {
					limit = {
						scope:recipient.ai_greed >= 25
					}
					add = ai_greed
				}
				if = {
					limit = {
						NOT = {
							scope:recipient.house = scope:actor.house
						}
					}
					multiply = 2
				}
			}
			desc = IS_CLAIMANT_TO_ACTORS_TITLES
		}
		#Actor has claims on recipient's titles.
		modifier = {
			scope:actor = {
				any_claim = {
					holder = scope:recipient
				}
			}
			add = 25
			desc = IS_CLAIMANT_TO_RECIPIENT_TITLES
		}
		modifier = {
			scope:recipient = {
				NOT = {
					any_claim = {
						holder = scope:actor
					}
				}
			}
			scope:recipient.house = scope:actor.house
			add = 15
			desc = IS_SAME_HOUSE_REASON
		}
		modifier = {
			scope:recipient = {
				has_opinion_modifier = {
					target = scope:actor
					modifier = declared_war
				}
			}
			add = -100
			desc = DECLARED_WAR_REASON
		}

		# Don't form new alliances with people already at war.
		modifier = {
			scope:actor = {
				is_at_war = yes

				# Since vassals and lieges can't call us into wars, this doesn't matter for us if we have that relationship. 
				NOR = {
					target_is_liege_or_above = scope:recipient
					target_is_vassal_or_below = scope:recipient
				}
			}
			add = -50
			desc = IS_AT_WAR_REASON
		}

		#Bonus from Tradition Astute Diplomats
		modifier = {
			scope:actor.culture = { has_cultural_parameter = facilitate_alliance_acceptance }
			add = 50 #should be in a file 00_traditions_values
			desc = TRADITION_ASTUTE_DIPLOMATS_ACCEPTANCE_MODIFIER
		}

		# The AI doesn't want to form alliances with AI vassals within its capital duchy, so it can revoke them
		modifier = {
			scope:actor = {
				is_ai = yes
				is_vassal_or_below_of = scope:recipient
				any_held_title = {
					tier = tier_county
					de_jure_liege = scope:actor.primary_title.title_capital_county.de_jure_liege
				}
			}
			scope:recipient = {
				is_ai = yes
			}
			add = -5000
		}

		# Feud
		modifier = {
			add = -25
			scope:recipient = {
				existing_feud_against_target_trigger = { TARGET = scope:actor }
			}
			desc = MARRY_FAMILY_FEUD_REASON
		}

		# Existing Alliances
		modifier = {
			add = {
				scope:actor = {
					every_ally = {
						add = -15
					}
				}
			}
			scope:actor = {
				any_ally = {
					count >= 2
				}
			}
			NAND = {
				scope:recipient = {
					government_has_flag = government_is_clan
					is_vassal_or_below_of = scope:actor
				}
			}
			desc = EXISTING_ALLIANCES_REASON
		}

		# Existing Alliances
		modifier = {
			add = {
				scope:recipient = {
					every_ally = {
						add = -15
					}
				}
			}
			scope:recipient = {
				any_ally = {
					count >= 2
				}
			}
			NAND = {
				scope:recipient = {
					government_has_flag = government_is_clan
					is_vassal_or_below_of = scope:actor
				}
			}
			desc = THEIR_EXISTING_ALLIANCES_REASON
		}
		
		modifier = {
			add = intimidated_reason_value
			scope:recipient = {
				target_is_liege_or_above = scope:actor
				has_dread_level_towards = {
					target = scope:actor
					level = 1
				}
			}
			desc = INTIMIDATED_REASON
		}
		modifier = {
			add = cowed_reason_value
			scope:recipient = {
				target_is_liege_or_above = scope:actor
				has_dread_level_towards = {
					target = scope:actor
					level = 2
				}
			}
			desc = COWED_REASON
		}
		
		modifier = {
			add = intimidated_external_reason_value
			scope:recipient = {
				NOT = { target_is_liege_or_above = scope:actor }
				has_dread_level_towards = {
					target = scope:actor
					level = 1
				}
			}
			desc = INTIMIDATED_REASON
		}
		modifier = {
			add = cowed_external_reason_value
			scope:recipient = {
				NOT = { target_is_liege_or_above = scope:actor }
				has_dread_level_towards = {
					target = scope:actor
					level = 2
				}
			}
			desc = COWED_REASON
		}

		# Unity modifiers
		evaluate_action_increasing_house_unity = {
			VALUE = 100
		}

		fp3_struggle_resist_allied_wars_modifier = yes
		
		# LOW LEGITIMACY
		modifier = {
			desc = "LOW_LEGITIMACY_REASON"
			scope:actor = {
				OR = {
					has_legitimacy_flag = reduced_alliance_acceptance
					has_legitimacy_flag = very_reduced_alliance_acceptance
					has_legitimacy_flag = massively_reduced_alliance_acceptance
				}
			}
			add = {
				value = 0
				if = {
					limit = { scope:actor = { has_legitimacy_flag = reduced_alliance_acceptance } }
					add = -25
				}
				if = {
					limit = { scope:actor = { has_legitimacy_flag = very_reduced_alliance_acceptance } }
					add = -50
				}
				if = {
					limit = { scope:actor = { has_legitimacy_flag = massively_reduced_alliance_acceptance } }
					add = -100
				}
			}
		}

		# HIGH LEGITIMACY
		modifier = {
			desc = "HIGH_LEGITIMACY_REASON"
			scope:actor = {
				OR = {
					has_legitimacy_flag = increased_alliance_acceptance
					has_legitimacy_flag = very_increased_alliance_acceptance
					has_legitimacy_flag = extra_increased_alliance_acceptance
				}
			}
			add = {
				value = 0
				if = {
					limit = { scope:actor = { has_legitimacy_flag = increased_alliance_acceptance } }
					add = 10
				}
				if = {
					limit = { scope:actor = { has_legitimacy_flag = very_increased_alliance_acceptance } }
					add = 25
				}
				if = {
					limit = { scope:actor = { has_legitimacy_flag = extra_increased_alliance_acceptance } }
					add = 50
				}
			}
		}
		
		# INFLUENCE
		modifier = {
			desc = INFLUENCE_REASON
			scope:influence = yes
			add = {
				value = 10
				multiply = scope:actor.influence_level
				min = 5
			}
		}
	}

	ai_potential = {
		is_at_war = no
	}
	
	ai_targets = {
		ai_recipients = dynasty
		max = 20
	}
	
	ai_targets = {
		ai_recipients = family
		max = 10
	}
	
	ai_targets = {
		ai_recipients = spouses
	}
	ai_frequency = 72
	
	ai_will_do = {
		base = 0
		
		modifier = {
			add = 100
			any_ally = {
				count < 2
			}
			scope:recipient = { # Don't propose alliances to tiny insignificant rulers just because
				OR = {
					current_military_strength >= scope:actor.eighty_percent_of_current_military_strength
					primary_title.tier >= scope:actor.primary_title.tier
					AND = {
						primary_title.tier >= tier_duchy
						scope:actor.primary_title.tier < tier_empire
					}
				}
			}
		}
		
		modifier = { # Clans always want alliances with Vassals
			add = 100
			scope:actor = {
				government_has_flag = government_is_clan
			}
			scope:recipient = {
				is_vassal_of = scope:actor
			}
		}

		# Unity modifiers
		evaluate_action_increasing_house_unity = {
			VALUE = 100
		}
		
		modifier = { # The AI shouldn't propose to their Lieges
			factor = 0
			is_liege_or_above_of = scope:recipient
		}
		
		modifier = { # The AI shouldn't propose to their Vassals unless Clan
			factor = 0
			scope:actor = {
				NOT = { government_has_flag = government_is_clan }
			}
			scope:recipient = {
				is_vassal_of = scope:actor
			}
		}
	}
}

perk_alliance_interaction = {
	category = interaction_category_diplomacy
	ai_min_reply_days = 4
	ai_max_reply_days = 9
	icon = alliance
	

	interface_priority = 60
	desc = perk_alliance_interaction_desc
	
	greeting = positive
	notification_text = PERK_NEGOTIATE_ALLIANCE_NOTIFICATION
	
	is_shown = {
		scope:recipient = {
			NOR = {
				this = scope:actor
				is_allied_to = scope:actor
				target_is_vassal_or_below = scope:actor # Can only sign with external rulers/peer vassals, though the alliance is preserved if they somehow become your vassals
				target_is_liege_or_above = scope:actor
				government_has_flag = government_is_herder
			}
			is_ruler = yes
			primary_title.tier > tier_barony
		}
		scope:actor = {
			has_perk = defensive_negotiations_perk
			is_landless_adventurer = no
			NOR = {
				government_has_flag = government_is_herder
				# This interaction is harder to push through than Negotiate Alliance, and does the same thing, so don't show both
				is_character_interaction_shown = {
					recipient = scope:recipient
					interaction = negotiate_alliance_interaction
				}
			}
		}
	}
	
	is_valid_showing_failures_only = {
		
		custom_description = {
			text = "already_has_an_alliance_deal"
			subject = scope:recipient
			scope:actor = {
				NOT = {
					any_ally = {
						has_opinion_modifier = {
							modifier = perk_negotiated_alliance_opinion
							target = scope:actor
						}
					}
				}
			}
		}
		
		scope:recipient = {
			is_playable_character = yes
			is_at_war = no
			is_imprisoned = no
		}
		
		scope:actor = {
			is_playable_character = yes
			is_at_war = no
			is_imprisoned = no
		}
		
		scope:recipient = { is_busy_in_events_localised = yes }
		
		custom_description = {
			text = "has_already_rejected_renegotiation"
			subject = scope:recipient
			scope:actor = {
				NOT = {
					has_opinion_modifier = {
						modifier = refused_alliance_opinion
						target = scope:recipient
					}
				}
			}
		}
	}
	
	can_send = {
		custom_description = { # Exploit prevention
			text = "has_sent_interaction_already"
			subject = scope:recipient
			scope:actor = {
				NOT = {
					has_pending_interaction_of_type = perk_alliance_interaction
				}
			}
		}
	}

	on_auto_accept = {
		scope:recipient = {
			trigger_event = char_interaction.0234
		}
	}

	#Use hook
	send_option = {
		is_valid = {
			scope:actor = {
				has_usable_hook = scope:recipient
			}
		}
		flag = hook	
		localization = GENERIC_SPEND_A_HOOK
	}
	should_use_extra_icon = {
		scope:actor = { has_usable_hook = scope:recipient }
	}
	extra_icon = "gfx/interface/icons/character_interactions/hook_icon.dds"
	
	# Admin Gov can spend Influence to increase AI acceptance towards other same-realm admin characters
	send_option = {
		is_shown = {
			scope:actor = {
				government_allows = administrative
			}
			scope:recipient = {
				top_liege = scope:actor.top_liege
				government_allows = administrative
			}
		}
		is_valid = {
			scope:actor = {
				influence >= major_influence_value
			}
			custom_tooltip = {
				text = ALREADY_USING_HOOK
				NOT = { scope:hook = yes }
			}
		}
		flag = influence	
		localization = SPEND_INFLUENCE
	}

	send_options_exclusive = no
		
	on_accept = {
		save_scope_value_as = { #So that we can show the opinion gain in the response event
			name = perk_alliance_interaction
			value = yes
		}
		scope:actor = {
			custom_tooltip = negotiate_alliance_effect_tooltip
			trigger_event = char_interaction.0235
			create_alliance = {
				target = scope:recipient
				allied_through_owner = scope:actor
				allied_through_target = scope:recipient
			}

			stress_impact = {
				shy = minor_stress_impact_gain
			}

			if = {
				limit = { scope:hook = yes }
				use_hook = scope:recipient
			}
			
			if = {
				limit = {
					scope:influence = yes
				}
				change_influence = major_influence_loss
			}
		}
		scope:recipient = { # This opinion modifier controls the interaction, and is removed when breaking the alliance in any way (though on_actions)
			add_opinion = {
				modifier = perk_negotiated_alliance_opinion
				target = scope:actor
			}
		}

		add_clan_unity_interaction_effect = {
			CHARACTER = scope:actor
			TARGET = scope:recipient
			VALUE = medium_unity_gain
			DESC = clan_unity_alliance.desc
			REVERSE_NON_HOUSE_TARGET = no
		}
	}
	
	on_decline = {
		scope:actor = {
			trigger_event = char_interaction.0236
		}
		scope:actor = {
			add_opinion = {
				modifier = refused_alliance_opinion
				target = scope:recipient
			}
		}

		add_clan_unity_interaction_effect = {
			CHARACTER = scope:actor
			TARGET = scope:recipient
			VALUE = minor_unity_loss
			DESC = clan_unity_alliance_refusal.desc
			REVERSE_NON_HOUSE_TARGET = no
		}
	}

	auto_accept = {
		custom_description = {
			text = "spending_hook"
			subject = scope:actor
			object = scope:recipient
			scope:hook = yes
		}
	}
	
	ai_accept = { # Similar to Negotiate Alliance, but without modifiers for Liege, being an heir, etc.
		base = -25

		# Tier Differences
		modifier = {
			scope:actor = {
				tier_difference = {
					target = scope:recipient
					value >= 1
				}
			}
			add = {
				value = 30
				if = {
					limit = {
						tier_difference = {
							target = scope:recipient
							value = 2
						}
					}
					add = 30
				}
				if = {
					limit = {
						tier_difference = {
							target = scope:recipient
							value = 3
						}
					}
					add = 60
				}
				if = {
					limit = {
						tier_difference = {
							target = scope:recipient
							value = 4
						}
					}
					add = 90
				}
				if = {
					limit = {
						tier_difference = {
							target = scope:recipient
							value = 5
						}
					}
					add = 120
				}
			}
			desc = RANK_DIFFERENCE_REASON
		}
		modifier = {
			scope:recipient = {
				has_relation_lover = scope:actor
			}
			add = {
				value = 25
			}
			desc = WE_ARE_LOVERS
		}			
		modifier = {
			scope:recipient = {
				has_relation_friend = scope:actor
			}
			add = {
				value = 25
			}
			desc = WE_ARE_FRIENDS
		}		
		modifier = {
			scope:actor = {
				tier_difference = {
					target = scope:recipient
					value <= -1
				}
			}
			add = {
				value = -30
				if = {
					limit = {
						tier_difference = {
							target = scope:recipient
							value = -2
						}
					}
					add = -30
				}
				if = {
					limit = {
						tier_difference = {
							target = scope:recipient
							value = -3
						}
					}
					add = -60
				}
				if = {
					limit = {
						tier_difference = {
							target = scope:recipient
							value = -4
						}
					}
					add = -90
				}
				if = {
					limit = {
						tier_difference = {
							target = scope:recipient
							value = -5
						}
					}
					add = -120
				}
			}
			desc = RANK_DIFFERENCE_REASON
		}
		opinion_modifier = { # More likely to accept if Recipient likes the Actor
			who = scope:recipient
			opinion_target = scope:actor
			multiplier = 0.25
			min = 0
			desc = AI_OPINION_REASON
		}
		opinion_modifier = { # Less likely to accept if Recipient dislikes the Actor
			who = scope:recipient
			opinion_target = scope:actor
			multiplier = 1
			max = 0
			desc = AI_OPINION_REASON
		}

		modifier = {
			add = 20
			scope:recipient = {
				has_trait = loyal
			}
			desc = INTERACTION_LOYAL
		}

		modifier = {
			add = 20
			scope:recipient = {
				has_trait = trusting
			}
			desc = INTERACTION_TRUSTING
		}

		modifier = {
			add = 5
			scope:recipient = {
				has_trait = craven
			}
			desc = INTERACTION_CRAVEN
		}

		modifier = {
			add = -20
			scope:recipient = {
				has_trait = paranoid
			}
			desc = INTERACTION_PARANOID
		}

		modifier = {
			add = -20
			scope:recipient = {
				has_trait = arrogant
			}
			desc = INTERACTION_ARROGANT
		}

		modifier = {
			add = -20
			scope:recipient = {
				has_trait = ambitious
			}
			desc = INTERACTION_AMBITIOUS
		}

		modifier = {
			add = -40
			scope:recipient = {
				has_trait = disloyal
			}
			desc = INTERACTION_DISLOYAL
		}

		modifier = {
			add = -10
			scope:recipient = {
				has_trait = fickle
			}
			desc = INTERACTION_FICKLE
		}

		# Rivalry
		modifier = { # Say no to rivals
			add = -100
			scope:recipient = {
				has_relation_rival = scope:actor
				NOT = { has_relation_nemesis = scope:actor }
			}
			desc = "ACTOR_RIVAL_TO_ME_REASON"
		}
		modifier = { # Say no to nemesis
			add = -300
			scope:recipient = {
				has_relation_nemesis = scope:actor
			}
			desc = "ACTOR_NEMESIS_TO_ME_REASON"
		}
		
		# Dynasty Prestige Levels
		modifier = {
			add = -5
			desc = AI_DYNASTY_PRESTIGE_REASON
			scope:actor = { has_dynasty = yes }
			NOT = { scope:actor.dynasty = scope:recipient.dynasty }
			scope:actor.dynasty = { dynasty_prestige_level = 0 }
		}
		modifier = {
			desc = AI_DYNASTY_PRESTIGE_REASON
			scope:actor = { has_dynasty = yes }
			NOT = { scope:actor.dynasty = scope:recipient.dynasty }
			scope:actor.dynasty = { dynasty_prestige_level >= 2 }
			add = {
				value = 10
				if = {
					limit = {
						scope:actor.dynasty = { dynasty_prestige_level >= 3 }
					}
					add = 5
				}
				if = {
					limit = {
						scope:actor.dynasty = { dynasty_prestige_level >= 4 }
					}
					add = 5
				}
				if = {
					limit = {
						scope:actor.dynasty = { dynasty_prestige_level >= 5 }
					}
					add = 5
				}
				if = {
					limit = {
						scope:actor.dynasty = { dynasty_prestige_level >= 6 }
					}
					add = 5
				}
				if = {
					limit = {
						scope:actor.dynasty = { dynasty_prestige_level >= 7 }
					}
					add = 5
				}
				if = {
					limit = {
						scope:actor.dynasty = { dynasty_prestige_level >= 8 }
					}
					add = 5
				}
				if = {
					limit = {
						scope:actor.dynasty = { dynasty_prestige_level >= 9 }
					}
					add = 5
				}
				if = {
					limit = {
						scope:actor.dynasty = { dynasty_prestige_level >= 10 }
					}
					add = 5
				}
			}
		}
		modifier = { #Faith compatibility
			scope:recipient.faith = {
				faith_hostility_level = {
					target = scope:actor.faith
					value > faith_fully_accepted_level
				}
			}
			NOT = {
				scope:recipient.ai_zeal <= -50
			}
			add = {
				value = -10
				if = {
					limit = {
						# Exempt certain struggle phases.
						NOT = { is_struggle_parameter_active_interfaith_marriages_available_between_involved_characters_trigger = yes }
					}
					if = {
						limit = {
							scope:recipient.faith = {
								faith_hostility_level = {
									target = scope:actor.faith
									value >= faith_astray_level
								}
							}
						}
						subtract = 20
					}
					if = {
						limit = {
							scope:recipient.faith = {
								faith_hostility_level = {
									target = scope:actor.faith
									value >= faith_hostile_level
								}
							}
						}
						subtract = 20
					}
					if = {
						limit = {
							scope:recipient.faith = {
								faith_hostility_level = {
									target = scope:actor.faith
									value >= faith_evil_level
								}
							}
						}
						subtract = 50
					}
					if = {
						limit = {
							scope:recipient.ai_zeal <= -25
						}
						divide = 2
					}
					else_if = {
						limit = {
							scope:recipient.ai_zeal >= 50
						}
						multiply = 2
					}										
					if = {
						limit = {
							scope:recipient.faith = { has_doctrine = doctrine_pluralism_pluralistic }
						}
						divide = 2
					}
				}
			}
			desc = MARRY_DIFFERENT_FAITH_REASON
		}

		modifier = { # A Recipient is more likely to agree to an alliance requested by his liege.
			add = 20
			scope:recipient = {
				target_is_liege_or_above = scope:actor
			}
			desc = LIEGE_REASON
		}
		
		modifier = {
	  	  	add = {
				value = 1
				subtract = {
					value = scope:recipient.max_military_strength # For foreign diplomacy, consider recipients max
					divide = { value = scope:actor.current_military_strength min = 1 }
				}
				multiply = 20
				min = -1000
				max = 100
	  		}
			desc = AI_MILITARY_BALANCE_REASON
		}
		# Recipient is a Claimant to Actor's titles.
		modifier = {
			scope:recipient = {
				any_claim = {
					holder = scope:actor
				}
				NOR = {
					is_heir_of = scope:actor
					ai_greed <= -25
				}
			}
			add = {
				value = -50
				if = {
					limit = {
						scope:recipient.ai_greed >= 25
					}
					add = ai_greed
				}
				if = {
					limit = {
						NOT = {
							scope:recipient.house = scope:actor.house
						}
					}
					multiply = 2
				}
			}
			desc = IS_CLAIMANT_TO_ACTORS_TITLES
		}
		#Actor has claims on recipient's titles.
		modifier = {
			scope:actor = {
				any_claim = {
					holder = scope:recipient
				}
			}
			add = 25
			desc = IS_CLAIMANT_TO_RECIPIENT_TITLES
		}
		modifier = {
			scope:recipient = {
				NOT = {
					any_claim = {
						holder = scope:actor
					}
				}
			}
			scope:recipient.house = scope:actor.house
			add = 15
			desc = IS_SAME_HOUSE_REASON
		}
		modifier = {
			scope:recipient = {
				has_opinion_modifier = {
					target = scope:actor
					modifier = declared_war
				}
			}
			add = -100
			desc = DECLARED_WAR_REASON
		}

		# Don't form new alliances with people already at war.
		modifier = {
			scope:actor = {
				is_at_war = yes

				# Since vassals and lieges can't call us into wars, this doesn't matter for us if we have that relationship. 
				NOR = {
					target_is_liege_or_above = scope:recipient
					target_is_vassal_or_below = scope:recipient
				}
			}
			add = -50
			desc = IS_AT_WAR_REASON
		}

		#Bonus from Tradition Astute Diplomats
		modifier = {
			scope:actor.culture = { has_cultural_parameter = facilitate_alliance_acceptance }
			add = 50 #should be in a file 00_traditions_values
			desc = TRADITION_ASTUTE_DIPLOMATS_ACCEPTANCE_MODIFIER
		}

		# The AI doesn't want to form alliances with AI vassals within its capital duchy, so it can revoke them
		modifier = {
			scope:actor = {
				is_ai = yes
				is_vassal_or_below_of = scope:recipient
				any_held_title = {
					tier = tier_county
					de_jure_liege = scope:actor.primary_title.title_capital_county.de_jure_liege
				}
			}
			scope:recipient = {
				is_ai = yes
			}
			add = -5000
		}

		# Feud
		modifier = {
			add = -25
			scope:recipient = {
				existing_feud_against_target_trigger = { TARGET = scope:actor }
			}
			desc = MARRY_FAMILY_FEUD_REASON
		}

		# Existing Alliances
		modifier = {
			add = {
				scope:actor = {
					every_ally = {
						add = -15
					}
				}
			}
			scope:actor = {
				any_ally = {
					count >= 2
				}
			}
			desc = EXISTING_ALLIANCES_REASON
		}

		# Existing Alliances
		modifier = {
			add = {
				scope:recipient = {
					every_ally = {
						add = -15
					}
				}
			}
			scope:recipient = {
				any_ally = {
					count >= 2
				}
			}
			desc = THEIR_EXISTING_ALLIANCES_REASON
		}
		
		modifier = {
			add = intimidated_reason_value
			scope:recipient = {
				target_is_liege_or_above = scope:actor
				has_dread_level_towards = {
					target = scope:actor
					level = 1
				}
			}
			desc = INTIMIDATED_REASON
		}
		modifier = {
			add = cowed_reason_value
			scope:recipient = {
				target_is_liege_or_above = scope:actor
				has_dread_level_towards = {
					target = scope:actor
					level = 2
				}
			}
			desc = COWED_REASON
		}
		
		modifier = {
			add = intimidated_external_reason_value
			scope:recipient = {
				NOT = { target_is_liege_or_above = scope:actor }
				has_dread_level_towards = {
					target = scope:actor
					level = 1
				}
			}
			desc = INTIMIDATED_REASON
		}
		modifier = {
			add = cowed_external_reason_value
			scope:recipient = {
				NOT = { target_is_liege_or_above = scope:actor }
				has_dread_level_towards = {
					target = scope:actor
					level = 2
				}
			}
			desc = COWED_REASON
		}

		# Unity modifiers
		evaluate_action_increasing_house_unity = {
			VALUE = 10
		}
		
		# INFLUENCE
		modifier = {
			desc = INFLUENCE_REASON
			scope:influence = yes
			add = {
				value = 10
				multiply = scope:actor.influence_level
				min = 5
			}
		}
	}

	ai_potential = {
		is_at_war = no
		any_ally = {
			count < 2
		}
	}
	
	ai_targets = {
		ai_recipients = neighboring_rulers
		max = 10
	}
	
	ai_targets = {
		ai_recipients = peer_vassals
		max = 5
	}
	ai_frequency = 72
	
	ai_will_do = {
		base = 100

		# Unity modifiers
		evaluate_action_increasing_house_unity = {
			VALUE = 100
		}
		
		modifier = { # Don't disturb the player with pointless asks
			add = -5000
			scope:recipient = {
				is_ai = no
			}
			NOR = {
				scope:actor.current_military_strength >= scope:recipient.current_military_strength
				scope:actor.primary_title.tier >= scope:recipient.primary_title.tier
			}
		}
		
		modifier = {
			add = -5000
			scope:recipient.current_military_strength <= 100
		}
	}
	
}

join_war_interaction = {
	category = interaction_category_diplomacy
	interface = interfere_in_war
	special_interaction = interfere_in_war_interaction
	popup_on_receive = yes
	desc = join_war_interaction_desc
	icon = alliance
	interface_priority = 29

	greeting = positive
	notification_text = join_war_interaction_notification

	is_shown = {
		scope:recipient = {
			is_at_war = yes
			NOR = {
				this = scope:actor
				is_at_war_with = scope:actor
			}
		}
	}

	is_valid_showing_failures_only = {

		# recipient is not in (any) war against your liege(s) or suzerain(s)
		trigger_if = {
			limit = { exists = scope:actor.liege }
			custom_description = {
				text = join_war_interaction_recipient_warring_with_my_liege
				subject = scope:recipient
				scope:actor = {
					NOT = {
						any_liege_or_above = {
							is_at_war_with = scope:recipient
						}
					}
				}
			}
		}

		# special exception for liege trying to put down his vassal's peasant revolt
		trigger_if = {
			limit = {
				scope:actor = {
					target_is_vassal_or_below = scope:recipient
				}
			}
			custom_description = {
				text = join_war_interaction_recipient_not_fighting_peasant_faction
				subject = scope:recipient
				scope:recipient = {
					any_war_enemy = {
						is_leading_faction_type = peasant_faction
					}
				}
			}
		}
		# if the target is neither a vassal-or-below, nor a liege-or-above, the recipient must either be an ally or defending against a qualifying holy war - unless the character is an adventurer in which case other rules apply
		trigger_else_if = {
			limit = {
				scope:actor = {
					NOT = { target_is_liege_or_above = scope:recipient }
				}
			}
			trigger_if = {
				limit = {
					scope:actor = { is_landless_adventurer = yes }
				}
				# bypass the requirements, unless you were invloved in a war and decided to leave it
				scope:recipient = {
					trigger_if = {
						limit = {
							any_character_war = {
								has_variable_list = left_voluntarily
							}
						}
						custom_tooltip = {
							text = laamp_war_change_sides_interaction.left_voluntarily
							is_target_in_variable_list = {
								name = left_voluntarily
								target = scope:actor	
							}
						}
					}
				}
			}
			trigger_else_if = {
				limit = {
					scope:recipient = {
						any_character_war = {
							OR = {
								#Must be either the same faith as the character being holy warred...
								scope:recipient.faith = scope:actor.faith
								#... Or, same religion, *and* you couldn't holy war them yourself.
								AND = {
									scope:recipient.faith.religion = scope:actor.faith.religion
									scope:actor.faith = {
										faith_hostility_level = {
											target = scope:recipient.faith
											value < religious_cb_enabled_hostility_level
										}
									}
								}
								#... Or, the target is your liege and they are being attacked for your held title.
								AND = {
									scope:recipient = { target_is_liege_or_above = scope:actor }
									casus_belli = {
										any_target_title = {
											holder = {
												OR = {
													this = scope:actor
													is_vassal_or_below_of = scope:actor
												}
											}
										}
											
									}
								}
							}
							any_war_defender = { this = scope:recipient }
						}
					}
				}
				# Deliberately blank; we've passed the trigger and making a desc out of this doesn't make sense
			}
			# Special case for Iberians helping each other after FP2 Conciliation ending after FP2 Conciliation ending
			trigger_else_if = {
				limit = {
					fp2_struggle_conciliation_recipient_actor_involved_trigger = yes
					scope:recipient = {
						any_character_war = {
							primary_defender = scope:recipient
							primary_attacker = { save_temporary_scope_as = attacker_temp }
							fp2_struggle_conciliation_scope_uninvolved_trigger = { SCOPE = scope:attacker_temp }
						}
					}
				}
				# Deliberately blank; we've passed the trigger and making a desc out of this doesn't make sense
			}

			# if the actor and recipient is in the same confederation, they can always come to each others' aid
			trigger_else_if = {
				limit = {
					exists = scope:recipient.confederation
					scope:actor = { is_member_of_confederation = scope:recipient.confederation }
				}
				scope:actor = { is_member_of_confederation = scope:recipient.confederation } # for localization only
			}

			# suzerains can always join their tributaries' defensive wars
			trigger_else_if = {
				limit = {
					scope:recipient = { 
						is_tributary_of_suzerain_or_above = scope:actor
					}
				}
				custom_tooltip = {
					text = join_war_interaction_recipient_tributary_no_defensive_war
					NOT = {
						scope:recipient = {
							any_character_war = {
								primary_defender = scope:recipient
							}
						}
					}
				}
			}
			# tributaries can always offer to join their direct suzerains' defensive wars
			trigger_else_if = {
				limit = {
					scope:actor = { 
						is_tributary_of = scope:recipient
					}
				}
				custom_tooltip = {
					text = join_war_interaction_recipient_tributary_no_defensive_war
					NOT = {
						scope:recipient = {
							any_character_war = {
								primary_defender = scope:recipient
							}
						}
					}
				}
			}

			# if we haven't already passed any of the qualified conditions above, then we need to be allies
			trigger_else = {
				custom_description = {
					text = join_war_interaction_recipient_not_ally
					subject = scope:recipient
					scope:recipient = {
						is_allied_to = scope:actor
					}
				}
			}
		}

		# recipient is liege
		trigger_else = {
			custom_description = {
				text = join_war_interaction_recipient_not_liege
				subject = scope:recipient
				scope:actor = {
					target_is_liege_or_above = scope:recipient
				}
			}
		}
		
		scope:actor = {
			trigger_if = {
				limit = {
					is_landless_adventurer = yes
				}
				custom_tooltip = {
					text = reinforce_soldiers_any_regiment_tt
					maa_regiments_count >= 1
				}
			}
		}

		# Adventurers can't join wars in places they're exiled from.
		custom_tooltip = {
			text = the_wake_1
			NOT = {
				is_laamp_exiled_from_province_trigger = {
					PROVINCE = scope:recipient.capital_province
					LAAMP = scope:actor
				}
			}
		}
	}
	
	can_be_picked = {
		scope:target ?= {

			# recipient is war leader
			is_war_leader = scope:recipient
			
			# special exception for tributaries and suzerains to fight for one another
			trigger_if = {
				limit = {
					OR = {
						scope:actor = {
							is_tributary_of_suzerain_or_above = scope:recipient
						}
						scope:recipient = {
							is_tributary_of = scope:actor
						}
					}
				}
				# if this is directed at a tributary, make sure they're the primary defender
				trigger_if = {
					limit = {
						scope:recipient = { is_tributary_of = scope:actor }
					}
					custom_tooltip = {
						text = join_war_interaction_recipient_tributary_not_primary_defender
						primary_defender = scope:recipient
					}
				}
				trigger_else = { always = yes }
			}

			#allow laamp_join_war_contract to bypass the requirements
			trigger_else_if = {
				limit = {
					scope:actor = {
						is_landless_adventurer = yes
					}
				}
			}

			# if holy war, recipient is defender of same faith
			# and not too far away
			trigger_else_if = {
				limit = { is_religious_war = yes }
				trigger_if = { #Only if you're not allies
					limit = {
						NOT = { scope:actor = { is_allied_to = scope:recipient } }
						NOR = {
							scope:recipient = { target_is_liege_or_above = scope:actor }
							casus_belli = {
								any_target_title = {
									exists = holder
									holder = {
										OR = {
											this = scope:actor
											is_vassal_or_below_of = scope:actor
										}
									}
								}
							}
						}
						# Special case for Iberians helping each other after FP2 Conciliation ending
						AND = {
							fp2_struggle_conciliation_recipient_actor_involved_trigger = no
							NOT = {
								fp2_struggle_conciliation_scope_uninvolved_trigger = { SCOPE = scope:target.primary_attacker }
							}
						} 
					}
					custom_description = {
						text = join_war_interaction_recipient_not_def_GHW
						subject = scope:recipient
						scope:recipient.faith.religion = scope:actor.faith.religion
						any_war_defender = { this = scope:recipient }
					}
				}
				casus_belli = {
					any_target_title = { save_temporary_scope_as = war_target }
				}
				custom_description = {
					text = join_war_interaction_recipient_too_distant
					subject = scope:war_target
					object = scope:war_target
					scope:actor = {
						realm_to_title_distance_squared = {
							title = scope:war_target
							value <= squared_distance_almost_massive # Approximately 1.5 HREs in distance away. Any further and does it really make sense for you to join what is essentially a regional power struggle?
						}
					}
				}
			}
			# If not holy war, special exception for liege trying to put down his vassal's peasant revolt.	
			trigger_else_if = {
				limit = {
					is_religious_war = no
					scope:actor = { target_is_vassal_or_below = scope:recipient }
					primary_defender = scope:recipient
					primary_attacker = {
						is_leading_faction_type = peasant_faction
					}
					# Special case for Iberians helping each other after FP2 Conciliation ending
					AND = {
						fp2_struggle_conciliation_recipient_actor_involved_trigger = no
						NOT = {
							fp2_struggle_conciliation_scope_uninvolved_trigger = { SCOPE = scope:target.primary_attacker }
						}
					}
				}
				always = yes
			}
			#Confederation members can choose to join each others' offensive wars
			trigger_else_if = {
				limit = {
					scope:actor = { NOT = { is_allied_to = scope:recipient } }
					exists = scope:recipient.confederation
				}
				scope:actor = {
					is_member_of_confederation = scope:recipient.confederation
				}
			}
			# ...otherwise, recipient must be an ally...
			trigger_else_if = {
				limit = {
					is_religious_war = no
					scope:actor = { NOT = { target_is_liege_or_above = scope:recipient } }
					# Special case for Iberians helping each other after FP2 Conciliation ending
					AND = {
						fp2_struggle_conciliation_recipient_actor_involved_trigger = no
						NOT = {
							fp2_struggle_conciliation_scope_uninvolved_trigger = { SCOPE = scope:target.primary_attacker }
						}
					}
				}
				custom_description = {
					text = join_war_interaction_recipient_not_ally
					subject = scope:recipient
					scope:actor = {
						is_allied_to = scope:recipient
					}
				}
				trigger_if = {
					limit = {
						scope:actor = {
							target_is_vassal_or_below = scope:recipient
							NOT = { is_allied_to = scope:recipient }
						}
					}
					# If they are an unallied vassal, inform the player that we could have aided them if this was a peasant revolt.
					custom_description = {
						text = join_war_interaction_recipient_not_fighting_peasant_faction
						subject = scope:recipient
						primary_defender = scope:recipient
						primary_attacker = {
							is_leading_faction_type = peasant_faction
						}
					}
				}
			}

			# ...or our liege.
			trigger_else_if = {
				limit = {
					is_religious_war = no
					scope:actor = { NOT = { is_allied_to = scope:recipient } }
				}
				# recipient is liege
				custom_description = {
					text = join_war_interaction_recipient_not_liege
					subject = scope:recipient
					scope:actor = {
						target_is_liege_or_above = scope:recipient
					}
				}
			}
			# making the default behavior explicit if no other trigger_(else_)ifs are valid
			trigger_else = {
				always = yes
			}
		}

		joiner_not_already_in_another_war_with_any_target_war_participants_trigger = {
			WARRIOR = scope:recipient
			JOINER = scope:actor
		}

		can_join_war_liege_vassal_check_trigger = { WARRIOR = scope:recipient JOINER = scope:actor }
	}

	is_highlighted = {
		scope:actor = {
			any_character_task_contract = {		
				has_task_contract_type = laamp_join_war_contract
				task_contract_employer = scope:recipient
			}
		}
	}

	on_accept = {
		scope:target ?= {
			hidden_effect = {
				set_called_to = scope:actor
			}
			if = {
				limit = { is_attacker = scope:recipient }
				add_attacker = scope:actor
				if = {
					limit = {
						OR = {
							scope:actor = {
								any_home_court_hostage = {
									warden ?= { is_defender_in_war = scope:target }
								}
							}
							scope:target = {
								any_war_defender = {
									any_warden_hostage = { home_court ?= scope:actor }
								}
							}
						}
					}
					custom_tooltip = hostage_execution_warning_tt
				}
			}
			else = {
				add_defender = scope:actor
				if = {
					limit = {
						OR = {
							scope:actor = {
								any_home_court_hostage = {
									warden ?= { is_attacker_in_war = scope:target }
								}
							}
							scope:target = {
								any_war_attacker = {
									any_warden_hostage = { home_court ?= scope:actor }
								}
							}
						}
					}
					custom_tooltip = hostage_execution_warning_tt
				}
			}
			if = {
				limit = {
					is_religious_war = yes
					scope:actor = {
						any_active_accolade = {
							has_accolade_parameter = acclaimed_knight_piety_from_battle
						}
					}
				}
				scope:actor = {
					add_piety = medium_piety_gain
				}
			}
			if = {
				limit = {
					is_religious_war = yes
					scope:actor = {
						any_active_accolade = {
							has_accolade_parameter = acclaimed_knight_piety_from_battle_high
						}
					}
				}
				scope:actor = {
					add_piety = major_piety_gain
				}
			}
		}
		scope:actor = {
			stress_impact = {
				craven = medium_stress_impact_gain
			}
			add_to_variable_list = {
				name = joined_as_ally
				target = scope:target
			}
		}
		scope:recipient = {
			show_as_tooltip = {
				if = {
					limit = {
						is_ai = yes
					}
					progress_towards_friend_effect = {
						CHARACTER = scope:actor
						OPINION = 0
						REASON = friend_alliance
					}
				}
				else = {
					hidden_effect = { #To nudge friendship
						if = {
							limit = {
								NOR = {
									has_relation_friend = scope:actor
									has_relation_potential_friend = scope:actor
								}
							}
							set_relation_potential_friend = scope:actor
						}
					}
				}
			}
		}

		# If we're a clan this interaction affects unity - but only when we attemp to aid a house member against a non-house member
		if = {
			limit = {
				exists = scope:target
				scope:target = {
					OR = {
						AND = {
							primary_attacker = scope:recipient
							scope:recipient.house = scope:actor.house
							primary_defender = {
								NOT = { house = scope:actor.house }
							}
						}
						AND = {
							primary_defender = scope:recipient
							scope:recipient.house = scope:actor.house
							primary_attacker = {
								NOT = { house = scope:actor.house }
							}
						}
					}
				}
			}
			add_clan_unity_interaction_effect = {
				CHARACTER = scope:actor
				TARGET = scope:recipient
				VALUE = medium_unity_gain
				DESC = clan_unity_join_war.desc
				REVERSE_NON_HOUSE_TARGET = no
			}
		}
	}

	on_decline = {
		scope:actor = {
			trigger_event = char_interaction.0236
		}
	}

	auto_accept = yes

	ai_accept = {
		base = 100 # everyone wants help
	}
}
